-- NewguildHomePagePanel

local ListViewVertical = require("app.widget.listview.ListViewVertical")
local NewguildMemberRender = import(".renders.NewguildMemberRender")
local NewguildHomeLeftRender = import(".renders.NewguildHomeLeftRender")

local ClassRef = app.mvc.createPanelClass(...)

local MAX_WORD_LEN = tonumber( GD:getSystemPar( "guildNameLength", 6 ) ) -- 名字最长字符数

function ClassRef.create(node)
	local sprite = ClassRef.extend(node)
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init()
	return sprite
end

function ClassRef:init(data)
	self.super.init(self, data)

	self._shownOffLineMember = true -- 默认不显示离线成员
	self._guildHomeLeftRender = NewguildHomeLeftRender.create(self._ui.left)--界面左边部分 公会信息

	self._showOffLineMemberBtn = self._ui.Panel_bom:getChildByName( "selected_btn" )
	self._showOffLineMemberBtn:setBright( not self._shownOffLineMember )
	self._showOffLineMemberBtn:addClickEventListener(function() -- 显示离线响应
		self._shownOffLineMember = not self._shownOffLineMember
		executeFunc(self._onOperate, "selecte_shownOffLine", self._shownOffLineMember)
		self._showOffLineMemberBtn:setBright( not self._shownOffLineMember )

	end)
	self:_initSearchPanel()


	local params = {
		renderClass = NewguildMemberRender,
		perNum = 1,
		cellEventProxy = function(eventName, params)
			executeFunc(self._onOperate, eventName, params)
		end
	}

	self._memberList = ListViewVertical.new( self._ui.ScrollView, params )
end

-- 搜索框 处理
function ClassRef:_initSearchPanel()
	local function onEditCallback(strEventName,sender)
		local edit = sender
		if strEventName == "changed" then
			local text = edit:getText()
			local ulen = string.ulen(text)
			if ulen > MAX_WORD_LEN then
				local newStr = string.usubstr(text,MAX_WORD_LEN)
				if text~=newStr then
					edit:setText(newStr)
					text = newStr
					display.pushToast(string.format(L("max_input_char"), MAX_WORD_LEN))
				end
			end
		end
	end

	local tf = self._ui.Panel_search:getChildByName("TextField")
	self._editText = display.replaceTextFieldWithEditBox(tf)
	self._editText:registerScriptEditBoxHandler(onEditCallback)

	self._searchCloseBtn = self._ui.Panel_search:getChildByName("Button_close")
	self._searchCloseBtn:addClickEventListener(function(sender)
		self._editText:setText("")
		self._searchBtn:setVisible( true )
		self._searchCloseBtn:setVisible( false )

		executeFunc(self._onOperate, "closeSearch_guild")
	end)

	self._searchBtn = self._ui.Panel_search:getChildByName("Button_enter")
	self._searchBtn:addClickEventListener(function(sender)
		local str = self._editText:getText()
		if string.len(str) > 0 then
			self._searchBtn:setVisible( false )
			self._searchCloseBtn:setVisible( true )

			executeFunc(self._onOperate, "search_guildMember", { str, self._shownOffLineMember } )
		end
	end)
end

-- 返回搜索内容
function ClassRef:getSearchText()
	return self._editText:getText()
end

-- 返回 显示离线成员 标记状态
function ClassRef:isShownOffLineMember()
	return self._shownOffLineMember
end

-- 刷新左侧基础信息
function ClassRef:refreshGuildInfo( myGuildInfo )
	self._guildHomeLeftRender:refreshGuildInfo(myGuildInfo)
end

function ClassRef:getGuildMemberList()
	return self._memberList
end

function ClassRef:onOperate( cb )
	self._onOperate = cb
	self._guildHomeLeftRender:onOperate(self._onOperate)
end

return ClassRef
